package com.DTC;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;

public class LoadData {
    public ArrayList<IrisEntry> irisTrain;       //数据集
    public ArrayList<IrisEntry> irisTest;        //数据集
    public int length=0;                  //数据集长度
    public int testLen=0;
    public int trainLen=0;
    public float trainRate=0.8f;        //训练集所占比例
    public LoadData(String path){
        ArrayList<IrisEntry> irisData = new ArrayList<IrisEntry>();
        irisTrain = new ArrayList<IrisEntry>();
        irisTest = new ArrayList<IrisEntry>();
        try{
            FileReader fileReader = new FileReader(path);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            while((line = bufferedReader.readLine())!= null){
                if(line.equals("")){
                    break;
                }
                IrisEntry irisEntry = new IrisEntry(line);
                irisData.add(irisEntry);
                length++;
            }
            bufferedReader.close();
            fileReader.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        // Collections.shuffle(irisData);
        Random rand = new Random();
        for (int i = 0; i < irisData.size(); i++) {
            int randomIndexToSwap = rand.nextInt(irisData.size());
            IrisEntry temp = irisData.get(randomIndexToSwap);
            irisData.set(randomIndexToSwap, irisData.get(i));
            irisData.set(i, temp);
        }

        int index = (int) Math.floor(length*trainRate); 
        trainLen = index;
        testLen = length-index;
        for(int i=0; i<index ;i++){
            irisTrain.add(irisData.get(i));      
        }
        for(int i=index; i<length ;i++){
            irisTest.add(irisData.get(i));
        }
    }
}
